// pages/shop/shop.js
// pages/project/jcssDetail/jcssDetail.js
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");
var common = require("../../utils/common.js");
const util = require("../../utils/util.js");

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    bground:"/pages/images/bground.png",
    pageIndex:1,
    pageSize:20,
    phone:"",
    points:0,
    hasPhone:true,
    goodsList:[],
    iconsList:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.initGoods();
  },

  onShow:function(){
    that.setData({
      userInfo:app.globalData.userInfo
    })
  },

  doSign:function(){
    let that = this,r = {openid:this.data.userInfo.openid,type:1,customerId:wx.getStorageSync('customerId')||this.data.userInfo.id}, _url = req.actions.ym+"integra/doSign"
    console.log(r)
    utils.myResquest(r,_url,function(res){
      if(res.data.code == '200'){
        wx.showToast({title: '签到成功',icon:"success",duration:500})
        that.setData({"userInfo.havaSigned":1,"userInfo.points":res.data.points});
        app.globalData.userInfo = that.data.userInfo
      }else{
        wx.showModal({title: res.data.msg})
      }
    });
  },

  onShow:function(){
    console.log("getApp().globalData.userInfo.points:"+getApp().globalData.userInfo.points)
    this.setData({points:getApp().globalData.userInfo.points});
    this.initGoods();
  },

  onAuthorizeSuccess:function(res){
    if(res.detail.state == 'success'){
      this.setData({phone:wx.getStorageSync('phone'),hasPhone:true})
    }
  },

  initGoods:function(){
    let r = {pageIndex:this.data.pageIndex,pageSize:this.data.pageSize}
    let _url = req.actions.ym+"goods/getGoodsList";
    let that = this;
    utils.myResquest(r,_url,function(res){
      that.setData({goodsList:res.data.rows});
    });
  },
  goPointRule:function(e){wx.navigateTo({url: './rule/rule'})},
  goPointRecords:function(e){
    let points = e.currentTarget.dataset.points,customerId=this.data.userInfo.id
    wx.navigateTo({url: "./integralDetail/integralDetail?points=" + points+"&customerId="+customerId})
  },
  onViewDetail:function(e){
    let goodsId = e.currentTarget.dataset.item.id;
    if(e.currentTarget.dataset.item.remain <= 0){
      wx.showModal({
        title: "温馨提示", // 提示的标题
        content: "库存不足"
      })
    }else{
      let personStr = encodeURIComponent(JSON.stringify(this.data.userInfo));
      wx.navigateTo({url: "/pages/shop/detail/productDetail?goodsId=" + goodsId+"&userInfo="+personStr});
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})